<template>
  <el-dialog
    title="添加角色"
    width="700px"
    :visible="dialogVisible"
    @close="delOK"
  >
    <el-form
      ref="roleForm"
      label-width="120px"
      :model="form_role_list"
      :rules="rules"
    >
      <el-form-item
        label="角色名称"
        prop="role_name"
      >
        <el-input
          v-model="form_role_list.role_name"
          style="width: 532px"
          placeholder="请输入角色名称"
          name="name"
        />
      </el-form-item>
      <el-form-item
        label="角色标识"
        prop="identify"
      >
        <el-input
          v-model="form_role_list.identify"
          style="width: 532px"
          placeholder="请输入角色标识"
        />
      </el-form-item>
      <el-form-item
        label="角色描述"
        prop="description"
      >
        <el-input
          v-model="form_role_list.description"
          type="textarea"
          style="width: 532px"
          placeholder="请输入角色描述"
        />
      </el-form-item>
      <el-form-item
        label="角色权限"
      >
        <el-tree
          ref="tree"
          node-key="id"
          :data="dataList"
          :props="defaultProps"
          show-checkbox
          :default-checked-keys="checkedkeys"
        />
      </el-form-item>
    </el-form>
    <el-row
      slot="footer"
      type="flex"
      justify="end"
    >
      <el-col :span="6">
        <el-button @click="delOK">取消</el-button>
        <el-button @click="bntOK">确定</el-button>
      </el-col>
    </el-row>
  </el-dialog>
</template>

<script>
import { newRole, modifyRoleDataAccordingToId } from '@/api/permission'
export default {
  name: 'RoleList',
  props: {
    dataList: {
      type: Array,
      required: true
    },
    checkedkeys: {
      type: Array,
      required: true
    },
    dialogVisible: {
      type: Boolean,
      required: true
    }
  },
  data() {
    const Form_role_list = {
      role_name: '',
      identify: '',
      description: '',
      authlist: []
    }
    return {
      defaultProps: {
        label: 'permission_name'
      },
      form_role_list: { // 表单数据
        ...Form_role_list
      },
      rules: { // 验证规则
        role_name: [
          { required: true, message: '请输入活动名称', trigger: 'blur' }
        ],
        identify: [
          { required: true, message: '爬爬爬', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '爬爬爬', trigger: 'blur' }
        ]
        // authlist: [
        //   { requried: true, message: '爬爬爬', trigger: 'change', type: Array }
        // ]
      }
    }
  },
  methods: {
    async bntOK() {
      try {
        await this.$refs.roleForm.validate()
        this.form_role_list.authlist = this.$refs.tree.getCheckedKeys()
        if (this.form_role_list.id) {
          // 编辑事件
          await modifyRoleDataAccordingToId(this.form_role_list)
        } else {
          // 新增事件
          await newRole(this.form_role_list)
        }

        this.$message.success('操作成功')
        this.$parent.getRoleManagementData()
        this.$emit('update:checkedkeys', [])
        this.$emit('update:dialog-visible', false)
      } catch (err) { console.log(err) }
    },
    delOK() {
      // 移除表单验证
      this.$refs.roleForm.resetFields()
      this.form_role_list = {
        ...this.Form_role_list
      }
      this.$emit('update:checkedkeys', [])
      this.$emit('update:dialog-visible', false)
    }
  }
}
</script>

<style>

</style>
